﻿Imports System.Data.SqlClient
Imports System.Data
Imports System.Text.RegularExpressions

Public Class Registration
    Inherits System.Web.UI.Page

    'Dim connectionString As String = "Data Source=(localdb)\Projects;Initial Catalog=CIS441WinterParkWeb;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False"
    Dim connectionString As String = "Data Source=SHANK-PC\SQLEXPRESS;Initial Catalog=WinterPark;Integrated Security=True;Connect Timeout=15;Encrypt=False;TrustServerCertificate=False"

    Dim con As New SqlConnection(connectionString)
    Dim con2 As New SqlConnection(connectionString)
    Dim cmd As SqlCommand
    Dim cmd2 As SqlCommand
    Dim MessageBox As Object

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    End Sub

    Protected Sub SubmitButton_Click(sender As Object, e As EventArgs) Handles SubmitButton.Click

        If Page.IsValid = True Then
            'customer information
            Dim Surname As String = LastNameTextBox.Text
            Dim FirstName As String = FirstNameTextBox.Text
            Dim MiddleName As String = MiddleNameTextBox.Text
            Dim Street As String = StreetTextBox.Text
            Dim City As String = CityTextBox.Text
            Dim State As String = StateTextBox.Text
            Dim ZipCode As String = ZipCodeTextBox.Text
            Dim Phone As String = PhoneTextBox.Text
            Dim BirthDate As Date = BirthDateTextBox.Text
            Dim EmailAddress As String = EMailTextBox.Text
            Dim EmergencyContact As String = EmergencyTextBox.Text
            Dim ECPhone As String = ECPhoneTextBox.Text
            Dim UserName As String = LoginTextBox.Text
            Dim Password As String = PasswordTextBox.Text
 
            ' get the next available customer ID number
            Dim CustomerTableDataSet As New DataSet
            Dim selectCustomerStatement As String = "SELECT * from CustomerTable"
            Dim CustomerDataAdapter As New SqlDataAdapter(selectCustomerStatement, con)
            CustomerDataAdapter.Fill(CustomerTableDataSet, "CustomerTable")
            Dim count As Integer = CustomerTableDataSet.Tables("CustomerTable").Rows.Count
            Dim drCustomer As DataRow
            drCustomer = CustomerTableDataSet.Tables("CustomerTable").Rows(count - 1)
            Dim CustomerId As String = drCustomer("CustomerId") + 1

            ' add the new customer data to the dataset
            Dim InsertCustomerStatement As String = "INSERT INTO CustomerTable VALUES ('" & _
                                    CustomerId & "', '" & _
                                    Surname & "', '" & _
                                    FirstName & "', '" & _
                                    MiddleName & "', '" & _
                                    Street & "', '" & _
                                    City & "', '" & _
                                    State & "', '" & _
                                    ZipCode & "', '" & _
                                    Phone & "', '" & _
                                    BirthDate & "', '" & _
                                    EmailAddress & "', '" & _
                                    EmergencyContact & "', '" & _
                                    ECPhone & "')"
            Dim InsertLoginStatement As String = "INSERT INTO Login (UserName,Password,ID,SecurityID) VALUES ('" & UserName & "','" & Password & "', " & CustomerId & ",3)"

            'How to retreive a session variable
            Dim employee_ID As String = Session("EmployeeID")

            'Try'
            'cmd.ExecuteNonQuery()
            'MsgBox("Registration Complete!")
            'Catch ex As System.Data.SqlClient.SqlException 'When ex.Number = 2627
            ' MsgBox("The user name already exists.", MsgBoxStyle.Information)
            ' Catch ex As Exception
            'MsgBox("An error occured trying to add the user. A duplicate user exists. The user has not been added.", MsgBoxStyle.Information)
            ' Finally
            ' con.Close()
            'End Try

            Using conn As New SqlConnection(connectionString)
                Using cmd As New SqlCommand(InsertCustomerStatement, conn)
                    conn.Open()
                    cmd.ExecuteNonQuery()
                End Using
                If conn.State = ConnectionState.Closed Then
                    conn.Open()
                End If
                Using cmd As New SqlCommand(InsertLoginStatement, conn)
                    cmd.ExecuteNonQuery()
                End Using

            End Using

            ' Dim CustomerUpdateDataAdapter As New SqlDataAdapter(InsertCustomerStatement, con)
            'CustomerUpdateDataAdapter.Update(CustomerTableDataSet, "CustomerTable")
            'CustomerUpdateDataAdapter.Fill(CustomerTableDataSet)

            con.Close()
            Response.Write("Congratulations, new user has been created.")
            'Server.Transfer("MainMenu.aspx")
        End If
    End Sub

    Private Sub ZipCodeTextBox_TextChanged(sender As Object, e As EventArgs) Handles ZipCodeTextBox.TextChanged
        If Not IsNumeric(ZipCodeTextBox.Text) Then
            Response.Write("Please re-enter zipcode.")
        Else

        End If
    End Sub
    'Protected Sub BirthYearTextBox_TextChanged(sender As Object, e As EventArgs) Handles BirthYearTextBox.TextChanged
    'Dim MyDate = BirthDateTextBox.Text + BirthDateTextBox.Text + BirthYearTextBox.Text
    'Dim Day As Integer = Date.Parse(AsString)AsDate
    'Dim Month As Integer = Integer.Parse(BirthDateTextBox.ToString())
    'Dim Year As Integer = Integer.Parse(BirthYearTextBox.ToString())

    'Dim MyDate As New Date(Year, Month, Day)
    'End Sub

    Protected Sub CancelButton_Click(sender As Object, e As EventArgs) Handles CancelButton.Click
        Server.Transfer("MainMenu.aspx")
    End Sub

    'Private Sub EMailTextBox_TextChanged(sender As Object, e As EventArgs) Handles EMailTextBox.TextChanged
    Private Sub is_valid_email(ByVal email_id As String)
        Dim matchRegex As New Regex("[a-zA-Z0-9_\-\.]+@[a-zA-Z0-9_\-\.]+\.[a-zA-Z]{2,5}")
        Dim matches As MatchCollection = matchRegex.Matches(email_id)
        If matches.Count = 0 Then
            Response.Write("Wrong email address format.") ' In Webserver
            'MessageBox.Show("Wrong email address format."); 'In Windows Form
        Else
            Response.Write("Email address format is ok.")    ' In Webserver
            'MessageBox.Show("Email address format is ok."); 'In Windows Form
        End If
    End Sub

    'Private Function ValidEmailAddress(ByVal emailAddress As String, ByRef errorMessage As String) As Boolean
    'Declaration

    ' Confirm there is text in the control. 
    '    If EMailTextBox.Text.Length = 0 Then
    '       errorMessage = "E-mail address is required."
    '      Return False
    '
    '   End If

    ' Confirm that there is an "@" and a "." in the e-mail address, and in the correct order. 
    '    If emailAddress.IndexOf("@") > -1 Then
    '       If (emailAddress.IndexOf(".", emailAddress.IndexOf("@")) > emailAddress.IndexOf("@")) Then
    '          errorMessage = ""
    '         Return True
    '    End If
    'End If

    '    errorMessage = "E-mail address must be valid e-mail address format." + ControlChars.Cr + _
    '     "For example 'someone@example.com' "
    '  Return False
    ' End Function

    ' Private Sub e_Validating(ByVal sender As Object, _
    ' ByVal e As System.ComponentModel.CancelEventArgs) Handles textBox1.Validating

    'Dim errorMsg As String
    '   If Not ValidEmailAddress(EMailTextBox.Text, errorMsg) Then
    ' Cancel the event and select the text to be corrected by the user.
    '      e.Cancel = True
    '     EMailTextBox.Select(0, EMailTextBox.Text.Length)

    ' Set the ErrorProvider error with the text to display.  
    '        Me.errorProvider1.SetError(EMailTextBox, errorMsg)
    '   End If
    '    End Sub

    Protected Sub LessonSignupButton_Click(sender As Object, e As EventArgs) Handles LessonSignupButton.Click
        Server.Transfer("Lesson.aspx")
    End Sub

    Protected Sub LockerSignUpButton_Click(sender As Object, e As EventArgs) Handles LockerSignUpButton.Click
        Server.Transfer("SeasonLocker.aspx")
    End Sub

    Protected Sub MainMenuButton_Click(sender As Object, e As EventArgs) Handles MainMenuButton.Click
        Server.Transfer("MainMenu.aspx")
    End Sub

    Protected Sub MaintenanceButton_Click(sender As Object, e As EventArgs) Handles MaintenanceButton.Click
        Server.Transfer("Maintenance.aspx")
    End Sub

    Protected Sub ReportsButton_Click(sender As Object, e As EventArgs) Handles ReportsButton.Click
        Server.Transfer("Reports.aspx")
    End Sub

    Protected Sub SeasonPassButton_Click(sender As Object, e As EventArgs) Handles SeasonPassButton.Click
        Server.Transfer("SeasonMembership.aspx")
    End Sub
End Class


